package tour.customer.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import tour.customer.service.TourService;
import tour.customer.to.MemberTO;

@Controller
@RequestMapping("/customer")
public class MemberController {
	@Autowired
	private TourService service;	
	
	//로인인 폼으로 수행을 넘기는 controller
		@RequestMapping(value="login.do", method=RequestMethod.GET)
		public String loginForm(){
			return "/customer/login_form.jsp";//View 이름
		}
		
		//로그인 처리
		//매개변수 - 요청파라미터를 받을 변수 선언. 변수명과 요청파라미터 명 연결해서 값이 대입된다.
		//매개변수의 이름고 요청파라미터의 이름이 다른 경우 @RequestParam 을 이용해 설정.
		@RequestMapping(value="login.do", method=RequestMethod.POST)
		public String login(String id, String password, HttpSession session, HttpServletRequest request) throws SQLException{
//			System.out.println("로그인 정보 : "+id+" - "+password);
//			//Model호출 - 로그인처리
//			//로그인 여부 체크용 attribute를 session에 binding - Attribute:Member객체
//			session.setAttribute("login_member", new Member(id,password));
			MemberTO mb = null;
			String url = null;
			if(id.equals("admin")){
				if(password.equals("1111")){
					mb = new MemberTO("admin", "1111","관리자");
					session.setAttribute("member", mb);
					url="/index.jsp";
				}else{
					url = "/error.jsp";
				}
			}else{
			mb = service.selectMemberInfoByIdController(id);
				if(mb!=null){//있는 ID
					
					if(password.equals(mb.getCustPassword())){//로그인 성공
						//HttpSession 생성
	
						session.setAttribute("member", mb);
						url = "/index.jsp";
					}else{//ID는 맞지만 pwd가 틀려서 실패
						request.setAttribute("error", "패스워드가 틀렸습니다. 확인 후 다시 로그인 하세요");
						url = "/error.jsp";
					}
				}else{//ID가 틀려서 실패
					request.setAttribute("error", "없는 ID입니다. 확인 후 다시 로그인하세요");
					url = "/error.jsp";
				}
			}
			
			return url;
		}
		
		@RequestMapping("logOut.do")
		public String logOut(HttpSession session) throws SQLException{
			session.invalidate();
			return "/index.jsp";
		}
		
		@RequestMapping("join.do")
		public String join(HttpServletRequest  request, HttpServletResponse response ) throws SQLException{
			String birth=request.getParameter("nyear")+request.getParameter("nmonth")+request.getParameter("nday");
			String email= request.getParameter("cemail")+"@"+request.getParameter("nmail");
			String phone=request.getParameter("nphone")+request.getParameter("nphone1")+request.getParameter("nphone2")+request.getParameter("nphone3");
			String address =request.getParameter("caddress")+request.getParameter("address");
			
			
			
			MemberTO mto=new MemberTO(request.getParameter("cid"),
					request.getParameter("cpwd"),  
					request.getParameter("cname"),
					request.getParameter("csex"),
					request.getParameter("cnick"),
					birth, 
					email,
					phone, 
					request.getParameter("chintq"),
					request.getParameter("chinta"),
					address,
					request.getParameter("czipcode"));

			service.insertMember(mto);
			
			return "/index.jsp";
		}
		
		//회원정보 수정
		@RequestMapping("/update.do")
		public String update(String password, String eMail, String phonNumber, String zipCode ,  String Address, HttpSession session) throws SQLException{
			MemberTO mt = (MemberTO) session.getAttribute("member");
			MemberTO m = new MemberTO(mt.getCustId(),password,eMail,phonNumber,Address,zipCode);
			System.out.println(m);
			service.updateMemberInfoController(m);
			
			return "/index.jsp";
		}
		
		@RequestMapping("/address.do")
		@ResponseBody
		public ArrayList address(String address) throws SQLException{
			 ArrayList list = service.address(address);
			 System.out.println(list);
			return  list;
		}
		
		
		@RequestMapping("/delete.do")
		public String deletembmebr(HttpSession session){
			Object ob= session.getAttribute("member");
			MemberTO member = (MemberTO)ob;
			
			try {
				service.deleteMember(member.getCustId());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			
			}
			return "/customer/member_leave.jsp";
			
		}
		
		@RequestMapping("/idCheck.do")
		@ResponseBody
		public ArrayList<Integer> idCheckMember(String cid) throws SQLException{
			
			MemberTO m = service.selectMemberInfoByIdController(cid);
			ArrayList<Integer> list = new ArrayList<Integer>();
			if(m==null){
				list.add(1);
			}else{
				list.add(2);
			}
			
			
			return list;
		}
		
		@RequestMapping("/searchId.do")
		public String searchId(String custName, String custBirthdayNumber, String custHintQ,  String custHintA, ModelMap map) throws Exception{
			
			String mb = service.searchId(new MemberTO(null,custName, custBirthdayNumber, custHintQ, custHintA));
			String url = null;
				
				if(mb!=null){
					map.put("id", mb);
					url = "/customer/search_success.jsp";
			}else{
				//System.out.println("id" + mb.getCustId());
				url = "/error.jsp";
			}
			
			return url;
		}
		
		@RequestMapping("/searchPassword.do")
		public String searchPassword(String custId, String custHintQ, String custHintA, ModelMap map) throws Exception{
			
			String mb = service.searchPassword(new MemberTO(custId,null ,null, custHintQ, custHintA));

			String url = null;
				
				if(mb!=null){
					map.put("id", custId);
					map.put("password", mb);
					url = "/customer/search_success.jsp";
			}else{
				url = "/error.jsp";
			}
			
			return url;
		}
		
		@RequestMapping("/list.do")
		@ResponseBody
		public ArrayList memberList() throws SQLException{
			
			ArrayList	list = service.memberList();
			System.out.println(list);
			return list;
		}
		
		@RequestMapping("/nick.do")
		@ResponseBody
		public ArrayList memberNickList(String nickname) throws SQLException{
			
			ArrayList	list = service.memberNickList(nickname);
			System.out.println(list);
			return list;
		}
		
}
